package com.lt.crm.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lt.crm.pojo.BasDict;
import com.lt.crm.pojo.SalChance;
import com.lt.crm.service.SalChanceService;
import com.lt.crm.service.SysUserService;
import javax.annotation.Resource;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/chance")
public class ChanceController {

    @Resource
    SalChanceService salChanceService;
    @Resource
    SysUserService sysUserService;

    @RequestMapping("/list")
    public String list(@RequestParam(required = false, defaultValue = "") String custName,
                       @RequestParam(required = false, defaultValue = "") String chcDesc,
                       @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                       Model model) {
//        Sort sort = new Sort(Sort.Direction.ASC, "chcId");
//        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
    Page<BasDict> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageIndex, 5);
        Page<SalChance> page2 = salChanceService.findByChcCustNameLikeAndChcDescLike(custName, chcDesc, page);
        model.addAttribute("chancePage", page2);
        model.addAttribute("custName", custName);
        model.addAttribute("chcDesc", chcDesc);
        return "chance/list";
    }

    @RequestMapping("/save")
    public String save(SalChance chance) {
        try{
            Date chcDueDate = chance.getChcDueDate();
            System.out.println(chcDueDate);
            salChanceService.saveOrUpdate(chance);
            return "redirect:/chance/list";
        }catch (Exception e){
            return "chance/add";
        }
    }

    @RequestMapping("/edit")
    public String edit(@RequestParam Integer chcId,Model model) {
        model.addAttribute("users",sysUserService.list());
        model.addAttribute("chance",salChanceService.getById(chcId));
        return "chance/edit";
    }

    @RequestMapping("/add")
    public String add(Model model) {
        model.addAttribute("users",sysUserService.list());
        return "chance/add";
    }

    @RequestMapping("/del/{chcId}")
    @ResponseBody
    public Map del(@PathVariable Integer chcId) {
        Map<String, String> map = new HashMap<>();
        try{
            salChanceService.removeById(chcId);
            map.put("delResult","true");
        }catch (Exception e){
            map.put("delResult","false");
        }
        return map;
    }

}
